﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MarginTrading.Entity;
using MarginTrading.Common;
using MarginTrading.Services;

namespace MarginTrading.Forms
{
    public partial class AddClient : Form
    {
        public AddClient()
        {
            InitializeComponent();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (validateInputs() == false)
            {
                return;
            }
            Client client = new Client();

            client.Code = textBoxClientIDPrefix.Text + "-" + textBoxClientID1.Text + "-" + textBoxClientID2.Text + "-" + textBoxClientID3.Text;
            client.Title = textBoxTitle.Text;
            client.Initials = textBoxInitials.Text;
            client.OtherNames = textBoxOtherNames.Text;
            client.Surname = textBoxSurname.Text;
            client.Address_1 = textBoxAddress1.Text;
            client.Address_2 = textBoxAddress2.Text;
            client.Telephone1 = textBoxTelephone.Text;
            client.Fax = textBoxFax.Text;
            client.Category = comboBoxAdvisorCode.Text;
            client.BankAccNo = textBoxAccNO.Text;

            Config c = ConfigServices.GetConfigByName(Constants.CONFIG_NAME_DEPLOYMENT_SIDE);
            if (c.Value == Constants.CONFIG_VALUE_DEPLOYMENT_SIDE_BROKER)
            {
                Config c1 = ConfigServices.GetConfigByName(Constants.CONFIG_NAME_COMPANY_CODE);
                client.Broker = c1.Value;
            }

            if (textBoxMarginLimit.Text.Length != 0)
            {
                client.MarginLimit = double.Parse(textBoxMarginLimit.Text);
            }

            client.RegDate = dateTimePicker1.Value.Date;
            if (checkBoxMarginClient.Checked == true)
            {
                client.MarginClient = 1;
                client.MTRegDate = dateTimePicker1.Value.Date;
            }
            else
            {
                client.MarginClient = 0;
            }
           
            try
            {
                ClientService.Add(client);
            }
            catch (System.Exception ex)
            {
                if (ex.Message.StartsWith("ORA-00001: unique"))
                {
                    MessageBox.Show("Client " + client.Code + " already exists", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    MessageBox.Show("Failed to create new client\nError: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                return;
            }

            if ((textBoxAccountBalance.Enabled == true) && (!string.IsNullOrEmpty(textBoxAccountBalance.Text)))
            {
                try
                {
                    DailyTransactionsServices.AddInitialAccBal(client.Code, double.Parse(textBoxAccountBalance.Text), false);
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("Failed to add initial account balance\nError: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

            MessageBox.Show("Client added successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            

            if (checkBoxDontClose.Checked == false)
            {
                Close();
            }
            else
            {
                textBoxClientID1.Text = "";
                textBoxClientID2.Text = "";
                textBoxClientID3.Text = "";
                textBoxClientIDPrefix.Text = "";
                textBoxTitle.Text = "";
                textBoxInitials.Text = "";
                textBoxOtherNames.Text = "";
                textBoxSurname.Text = "";
                textBoxAddress1.Text = "";
                textBoxAddress2.Text = "";
                textBoxFax.Text = "";
                textBoxTelephone.Text = "";
                textBoxMarginLimit.Text = "";
                textBoxAccountBalance.Text = "";
                checkBoxMarginClient.Checked = false;
                textBoxAccNO.Text = "";

                Config config = ConfigServices.GetConfigByName(Constants.CONFIG_NAME_DEPLOYMENT_SIDE);
                if (config.Value == Constants.CONFIG_VALUE_DEPLOYMENT_SIDE_AGENT)
                {
                    textBoxClientIDPrefix.Focus();
                    textBoxClientIDPrefix.TabIndex = 0;
                }
                else if (config.Value == Constants.CONFIG_VALUE_DEPLOYMENT_SIDE_BROKER)
                {
                    Config config2 = ConfigServices.GetConfigByName(Constants.CONFIG_NAME_COMPANY_CODE);
                    textBoxClientIDPrefix.Text = config2.Value;
                    textBoxClientID1.Focus();
                }
            }
        }

        private bool validateInputs()
        {
            if (textBoxClientID1.Text == ""
                || textBoxClientID2.Text == ""
                || textBoxClientID3.Text == ""
                || textBoxClientIDPrefix.Text == "")
            {
                MessageBox.Show("Enter client code", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (textBoxMarginLimit.Text != "")
            {
                double fMarginLimit = 0;
                if (double.TryParse(textBoxMarginLimit.Text, out fMarginLimit) == false)
                {
                    MessageBox.Show("Invalid margin limit", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return false;
                }
            }

            return true;
        }

        private void textBox16_TextChanged(object sender, EventArgs e)
        {
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {
        }

        private void textBoxClientID1_TextChanged(object sender, EventArgs e)
        {
            if (textBoxClientID1.Text.Length == 9)
            {
                textBoxClientID2.Focus();
            }
        }

        private void textBoxClientID2_TextChanged(object sender, EventArgs e)
        {
            if (textBoxClientID2.Text.Length == 2)
            {
                textBoxClientID3.Focus();
            }
        }

        private void textBoxClientIDPrefix_TextChanged(object sender, EventArgs e)
        {
            if (textBoxClientIDPrefix.Text.Length == 3)
            {
                textBoxClientID1.Focus();
            }
        }

        private void AddClient_Load(object sender, EventArgs e)
        {
            try
            {
                ClientCategoryCollection categoryCollection = ClientCategoryServices.GetClientCategoryCollection();
                for (int iI = 0; iI < categoryCollection.Count; iI++)
                {
                    comboBoxAdvisorCode.Items.Add(categoryCollection[iI].Code);
                }
                
                Config config = ConfigServices.GetConfigByName(Constants.CONFIG_NAME_DEPLOYMENT_SIDE);
                if (config.Value == Constants.CONFIG_VALUE_DEPLOYMENT_SIDE_AGENT)
                {
                    textBoxClientIDPrefix.Focus();
                    textBoxClientIDPrefix.TabIndex = 0;
                }
                else if (config.Value == Constants.CONFIG_VALUE_DEPLOYMENT_SIDE_BROKER)
                {
                    Config config2 = ConfigServices.GetConfigByName(Constants.CONFIG_NAME_COMPANY_CODE);
                    textBoxClientIDPrefix.Text = config2.Value;
                }

                comboBoxAdvisorCode.SelectedIndex = 0;
            }
            catch (System.Exception ex)
            {
            }
        }

        private void checkBoxMarginClient_CheckedChanged(object sender, EventArgs e)
        {
            textBoxMarginLimit.Enabled = checkBoxMarginClient.Checked;
            textBoxAccountBalance.Enabled = checkBoxMarginClient.Checked;
        }
    }
}
